### FILE="Main.annotation"
# Copyright:	Public domain.
# Filename:	MODE_SWITCHING_AND_MARK_ROUTINES.agc
# Purpose:	Part of the source code for Solarium build 55. This
#		is for the Command Module's (CM) Apollo Guidance
#		Computer (AGC), for Apollo 4.
# Assembler:	yaYUL --block1
# Contact:	Jim Lawton <jim DOT lawton AT gmail DOT com>
# Website:	www.ibiblio.org/apollo/index.html
# Page scans:	www.ibiblio.org/apollo/ScansForConversion/Solarium055/
# Mod history:	2009-10-06 JL	Created.

## Page 190

#	THE FOLLOWING SET OF PROGRAMS ARE USED TO SELECTTHE VARIOUS MODES OF THE IMU AND OPTICS. THE FOLLOWING 
# MODES ARE POSSIBLE:
#
#	IMUZERO     ZEROS IMU CDUS.
#	IMUCOARS    COARSE ALIGNS IMU.
#	IMURECOR    RETURNS IMU FROM FINE ALIGN TO COARSE ALIGN
#	IMUFINE     PREPARES TO PULSE TORQUE THE GYROS.
# IMUFINNW     SWITCHES TO FINE WITH NO DELAY FOR IRIG CENTERING-DONT  ENTER FROM COARSE ALIGN MODE.
#	IMUATTC     USE IMU FOR S/C ATTITUDE CONTROL.
#	IMUREENT    USE IMU FOR ROLL RE-ENTRY.
#	IMULOCK     LOCKS IMU CDUS.
#
#	IMUFINIS(**)RELEASES IMU
#	IMUSTALL(*) IMU MODE IDLING AND ERROR CHECKING.



#	OPTZERO     ZERO OPTICS CDUS.
#	OPTCOARS    DUMMY OPTICS COARSE-ALIGN MODE.
#	OPTTRKON    OPTICS TRACKER ON.
#
#	SCTMARK     REQUEST N SCANNING TELESCOPE MARKS.
#	SXTMARK     REQUEST N SEXTANT MARKS.
#	MKRELEAS(**)RELEASE MARK SYSTEM.
#
#	OPTFINIS(**)RELEASE OPTICS.
#	OPTSTALL(*) OPTICS MODE IDLING AND ERROR CHECKING.



#	OPTICS AND IMU MODE ROUTINES MAY BE USED CONCURRENTLY.
#
#	IN ADDITION, A ROUTINE WHICH INCREMENTS ANY DESIRED 2S COMPLEMENT ANGLE BY AN INPUT AMOUNT IS
# INCLUDED (CDUINC).



#	CALLING SEQUENCE IS AS FOLLOWS:
#
# L-1	CAF	NO.MARKS	(FOR SCTMARK AND SXTMARK ONLY).
# L	TC	BANKCALL
# L+1	CADR	(SUBRO)		ANY OF THE 14 MODE ROUTINES OR CDUINC.



#	(*)	THE STALL ROUTINES ARE CALLED TO TERMINATE ANY MODE REQUEST. THE REQUESTING JOB IS STALLED UNTIL
# THE MODE-WITCHING IS COMPLETE (WITH VAC-AREA PROTECTED) AND RETURN IS TO L+2 IF UNSUCCESSFUL AND L+3 IF THE
# DESIRED MODE WAS SUCCESSFULLY ACHIEVED.
#
#	(**)	NO STALL ROUTINE IS NECESSARY IN CONJUNCTION WITH THIS REQUEST.

## Page 191

#	SPECIAL DP TIME COUNTER READING ROUTINE.

		BANK	1
READTIME	INHINT			# ENTRY IF UNDER EXECUTIVE.
		CS	TIME2		# ENTRY IF IN INTERRUPT.
		TS	RUPTSTOR
		CS	TIME1
		TS	RUPTSTOR +1
		CCS	A		# IF MINOR PART ZERO, MAJOR PART COULD
		TC	Q
		CCS	A
		TC	Q
		CS	TIME2		# UP, SO READ IT AGIN.
		TS	RUPTSTOR
		TC	Q
		
#	ROUTINE TO READ FINE TIME IN IN2 EVEN IF IT CHANGES AS ITS BEING READ. CALL IN INHINT.

FINETIME	XCH	IN2		# MAY BE CALLED UNDER EXEC OR RUPT.
		XCH	IN2
		MASK	FINEMASK	# NOTE THAT THE OR OF TWO STATES A AND B
		TS	ITEMP1		# IS NOT LESS THAN EITHER A OR B.
		XCH	IN2
		MASK	FINEMASK
		TS	ITEMP2
		COM			# SELECT THE MINIMUM OF THE LAST TWO 
		AD	ITEMP1		# STATES READ.
		CCS	A
		XCH	ITEMP2
		TC	Q
		NOOP
		XCH	ITEMP1
		TC	Q
		
#	SUBROUTINE TO ZERO IMU CDU COUNTERS.

ZEROICTR	CAF	ZERO		# USED BY AUTOMATIC AND MANUAL ZEROING
		TS	CDUX		#  ROUTINES.
		TS	CDUY
		TS	CDUZ
		TC	Q



LODSAMPT	CS	RUPTSTOR	# TIME IS SNATCHED IN RUPT FOR NOUN 65
		XCH	SAMPTIME
		CS	RUPTSTOR +1
		XCH	SAMPTIME +1
		TC	Q
		
## Page 192

#	SETZLIT SETS THE ZERO ENCODE LAMP ACCORDING TO THE DESIRED-MODE REGISTERS DESKSET AND DESOPSET.

SETZLIT		XCH	Q
		TS	ITEMP1
		
		CCS	DESKSET
		TC	+4		# COMPUTER COMMANDING-MAY BE ZEROING.
		TC	OPTZTEST	# NOT MANUAL ZERO.
TUNRONZ		TC	ZLITON		# MANUAL ZEROING MODE.
		TC	ITEMP1
		
 +4		AD	ONE
		MASK	BIT1
		CCS	A
		TC	TURNONZ		# COMPUTER COMMANDING ZERO-ENCODER
		
OPTZTEST	CCS	DESOPSET
		TC	+3		# MAY BE COMMANDING OPTICS ZERO.
		TC	ZLITOFF		# NOT MANUAL ZERO-TURN LIGHT OFF.
		TC	TURNONZ		# MANUAL OPTICS
		
 +3		AD	ONE
		MASK	BIT12
		CCS	A
		TC	TURNONZ
		
ZLITOFF		CS	ZLITBITS	# TURN OFF ZERO ENCODE LAMP.
		MASK	DSPTAB +11D
		AD	BIT15
		TS	DSPTAB +11D
		TC	ITEMP1
		
ZLITON		CS	ZLITBITS	# TURN ON ZERO ENCODE LAMP.
		MASK	DSPTAB +11D
		AD	ZLITBITS
		TS	DSPTAB +11D
		TC	Q
		
ZLITBITS	EQUALS	OCT40020	# CS CYR IN DMP.

## Page 193

#	IMU ZEROING ROUTINE.

		SETLOC	30000
		
IMUZERO		CS	ONE		# DISABLE CDU DRIVE
		TS	CDUIND		#  BY SETTING CDUIND NEGATIVE.
		
		TS	SETKANDC	# GO TO SUBROUTINE TO SWITCH C RELAYS
		OCT	00051		#  = C(DESKSET)=COMP CONT+ZERO+FINE
		OCT	40011		#  = C SETTING FOR FINE + ZERO
		TC	ZLITON		# TURN ON ENCODER ZEROING LAMP
		
		CS	BIT4		# DISABLE IMU FAIL FOR 5 SEC IN CASE WE
		MASK	OLDERR		# JUST CAME OUT OF COARSE ALIGN/
		AD	BIT4
		TS	OLDERR
		CAF	BIT10
		TC	WAITLIST
		CADR	IFAILOK
		
		CAF	35SECS		# SET A WAITLIST CALL FOR 35 SECONDS
		TC	WAITLIST	#  SO  THOSE SHAFTS WILL HAVE TIME TO GET
		CADR	IMUZEROD	# THERE.
MODEEXIT	RELINT			# GENERAL EXIT FROM MODE SWITCH PROGS
		TC	SWRETURN	#  WHICH LEAVES VIA SWCALL EXIT
		
IMUZEROD	TC	ZEROICTR	# GO AND ZERO X, Y, AND Z COUNTERS

		TC	KCHECK		# SEE IF SYSTEM STILL FUNCTIONING OK.
MOREZERO	TC	SETKANDC +1	# CALL FOR 10 SECONDS OF FINE ALIGN TO
		OCT	00050		#  ALLOW CDUS TO GO AT LEAST 90 DEGREES
		OCT	40010		#  OF THE WAY HOME UNLESS THE PLATFORM IS
		CAF	10SECS		#  SITTING AT ANY FALSE NULLS.
		TC	WAITLIST
		CADR	ZEROATTC	# WE WILL GO INTO ATTITUDE CONTROL NEXT.
		TC	TASKOVER
		
ZEROATTC	TC	KCHECK		# USUAL SYSTEM CHECK.
		TC	SETKANDC +1	# COMMAND ATTITUDE CONTROL (WITHOUT ATTC
		OCT	00060		#  LAMP LIT) SO THAT WE CAN DRIVE THE CDUS
		OCT	41000		# (LIGHT ON AFTER ALL) AWAY FROM FALSE NUL
		CAF	320MS
		TC	WAITLIST
		CADR	ZERODRVE	# WAIT FOR MODE SWITCH BEFORE ENABLING CDU
		TC	TASKOVER	#  DRIVE IN T4RUPT.
## Page 194

ZERODRVE	TC	KCHECK		# VERIFY SWITCH TO ATTITUDE CONTROL.
		CAF	QUARTER		# WE WILL DRIVE THE CDUS TOWARD 45 DEGREES
		TS	THETAD		#  FOR 2SECS (ABOUT 10 DEGREES OF MOVEMENT
		TS	THETAD +1	#  AT MOST). THIS WILL DRIVE ANY CDUS OFF
		TS	THETAD +2	#  FALSE NULLS WITHOUT MOVING THE PLATFORM
		CAF	ZERO
		TS	CDUIND		# CDU LOOPS ARE NOW ENABLED.
		CAF	2SECS
		TC	WAITLIST
		CADR	REZFINE		# GO BACK TO FINE ALIGN WHEN DONE.
		TC	TASKOVER
		
REZFINE		TC	KCHECK		# VERIFY MODE SWITCH AS USUAL.
		TC	SETKANDC +1	# CALL FOR 20 SECONDS OF FINE ALIGN TO
		OCT	00050		#  ALLOW ENOUGH TIME FOR THE CDUS TO FIND
		OCT	40010		#  THE GIMBALS.
		CS	ONE
		TS	CDUIND		# CDU LOOPS NOW DISABLED.
		CAF	20SECS
		TC	WAITLIST
		CADR	IMUFINED	# CHECK FORPRESENCE OF IMU OR CDU FAILS.
		TC	TASKOVER



KCHECK		CCS	WASKSET		# RETURN TO CALLER IF SYSTEM OK - SET UP
		TC	Q		#  ERROR RETURN AT ENDIMU OTHERWISE.
		TC	ENDIMU
		TC	Q
		TC	ENDIMU
		
90SECS		DEC	90.00 E2
45SECS		DEC	45.00 E2	# TIME FOR IMUCDU COARSE ALIGN.
35SECS		DEC	35.00 E2	# TIME FOR IMUCDU ZERO ENCODER.
20SECS		DEC	20.00 E2
10SECS		DEC	10.00 E2
2SECS		DEC	2.00 E2
320MS		DEC	.32 E2

## Page 195

# IMU COARSE ALIGN PROGRAM

IMUCOARS	TC	SETKANDC	# SET DESIRED C AND K RELAY SETTINGS.
		OCT	00042		#   COMPUTER CONTROL . COARSE ALIGN.
		OCT	40002		#   C RELAY COARSE ALIGN.
		
		TC	SETZLIT		# SERVICE THE ZERO ENCODER LAMP
		
		CAF	320MS		# SET WAITLIST CALL FOR 320 M.S. TO
		TC	WAITLIST	#  ACCOMODATE CONTACT BOUNCE + T4RUPT LAG
		CADR	BEGINCOM
		TC	MODEEXIT
		
		
		
RECOARSD	CS	CDUX		# GOING BACK INTO COARSE ALIGN FROM FINE
		COM			# ALIGN. PUT CDU COUNTER VALUES INTO
		TS	THETAD		# DESIRED ANGLE REGISTERS AND ENABLE THE
		CS	CDUY		# T4RUPT CDU DRIVE.
		COM
		TS	THETAD +1
		CS	CDUZ
		COM
		TS	THETAD +2
		
BEGINCOM	TC	KCHECK		# VERIFY CORRECTNESS OF PRESENT MODE.
STARTCRS	CAF	ZERO		# ENABLE CDU LOOP CLOSURE
		TS	CDUIND
		
		CAF	45SECS
		TC	WAITLIST	#  CDU LOOPS TO SETTLE
		CADR	COARSDON
		TC	TASKOVER
		
## Page 196

# IMU FINE ALIGN PROGRAM

IMURECOR	TC	SETKANDC	# GO INTO COARSE ALIGN FROM FINE ALIGN.
		OCT	00042		# (SEE REMARKS ON RECOARSD FOR FURTHER
		OCT	40002		#   DETAILS).
		CAF	320MS		# USUAL CONTACT CLOSURE AND SAMPLE TIME
		TC	WAITLIST
		CADR	RECOARSD
		TC	DISEXIT		# DISABLE GYRO ACTIVITY.
		
IMUFINE		TC	SETKANDC	# SET UP C RELAY PATTERN AND DESIRED K
		OCT	00050		#  DES K = COMPUTER CONTROL + FINE
		OCT	40010		#      C = FINE ALIGN
		
		CS	BIT4		# COMES THROUGH HERE AFTER EVERY COARSE
		MASK	OLDERR		# ALIGN. SET BIT 4 OF OLDERR TO INHIBIT
		AD	BIT4		# IMU FAIL FOR ANOTHER 5 SECONDS.
		TS	OLDERR
		
		CAF	BIT10
		TC	WAITLIST
		CADR	IFAILOK
		
		CAF	90SECS		# SET WAITLIST CALL FOR 90 SEC. TO ALLOW
		TC	WAITLIST	#  GYROS TO RE-CENTER BEFORE IRIG PULSE
		CADR	IMUFINED	#  TORQUING
DISEXIT		CS	ONE		# DISABLE T4 IMU (CDU OR GYRO) ACTIVITY.
		TS	CDUIND
		TC	MODEEXIT



IMUFINNW	TC	TSTIMACT

		TC	SETKANDC
		OCT	00050
		OCT	40010
		CAF	320MS
		TC	DISEXIT -2
		
## Page 197

IMUFINED	TC	FLAG1DWN
		OCT	02000
		
		TC	ENDIMU
		
		CAF	BIT12		# NO IMU FAILS, PLEASE.
		MASK	OLDERR
		CCS	A
		TC	FAILEND
		TC	GOODEND
		
IFAILOK		CS	BIT4		# ENABLE IMU FAIL 5 SECONDS AFTER COMING
		MASK	OLDERR		# OUT OF COARSE ALIGN.
		TS	OLDERR
		TC	TASKOVER
		
## Page 198

# CDU LOCK PROGRAM

IMULOCK		TC	SETKANDC	# SET APPROPRIATE K AND C CONFIGS.
		OCT	00044
		OCT	40004
		
		CAF	320MS
		TC	WAITLIST	#  PLACE.
		CADR	IMULOCKD
		TC	DISEXIT
		
IMULOCKD	TC	ENDIMU
		TC	GOODEND
		
COARSDON	EQUALS	IMULOCKD

## Page 199

# IMU RE-ENTRY AND ATTITUDE CONTROL PROGRAMS

IMUREENT	TC	TSTIMACT
		TC	SETKANDC
		OCT	00140
		OCT	42000
		TC	ENABEXIT



IMUATTC		TC	TSTIMACT
		TC	SETKANDC
		OCT	00060
		OCT	41000
		
ENABEXIT	CAF	320MS
		TC	WAITLIST
		CADR	ENABLE
		TC	DISEXIT		# DISABLE POSSIBLE GYRO ACTIVITY.
		
ENABLE		TC	FLAG1DWN
		OCT	02000
		
		TC	ENDIMU
		
		CAF	ZERO		# ENABLE CDU LOOPS
		TS	CDUIND
		TC	GOODEND
					# NOTICE THAT NO INSPECTION OF CDU AND IMU
					# ERROR SIGNALS IS MADE SINCE IN THESE
					# MODES THE MAIN PROGRAM MUST CHECK THE
					# STATUS OF THESE SIGNALS PERIODICALLY.



#	IMUFINIS - TO BE CALLED AT THE END OF COMPUTER-CONTROLLED MODE SWITCHING. PLACES MODE SAMPLING INTO
# MANUAL CONTROL, SWITCHES THE SYSTEM TO FINE ALIGN, AND DISABLES THE T4RUPT CDU DRIVE. NO CALL TO IMUSTALL IS
# REQUIRED HERE.

IMUFINIS	TC	SETKANDC
		OCT	00000		# 0 MEANS COMPUTER IS NO LONGER IN CONTROL
		OCT	40010		# FINE ALIGN C-RELAY CODE.
		TC	DISEXIT
		
## Page 200

#	SUBROUTINE USED BY MODE-SWITCHING PROGRAMS TO SET DESIRED K- AND C-RELAY SETTINGS.

SETKANDC	INHINT			# INHINT AND EXIT WITH INTERRUPT INHIBITED
		INDEX	Q
		CAF	0
		TS	DESKSET		# DESIRED K-RELAY SETTING AS READ IN IN3.
		
		CAF	OFFMSK		# SET C-RELAYS FOR ZERO ENCODER, COARSE
		MASK	DSPTAB +11D	#   ALIGN, FINE ALIGN, LOCK CDU, ROLL
		INDEX	Q		#   RE-ENTRY, AND ATTITUDE CONTROL LAMP.
		AD	1
		TS	DSPTAB +11D
		
		INDEX	Q
		TC	2		# RETURN TO CALLER IN INHINT.
		
OFFMSK		OCT	34760

## Page 201

#	WHEN A MODE-SWITCHING TASK IS DISPATCHED TO INDICATE THE END OF A MODE-SWITCH, ENDIMU (OR ENDOPT) IS
# CALLED TO WAKE UP ANY JOB IMUSTALL (OPTSTALL) MIGHT HAVE PUT TO SLEEP AND THEN CHECK WASKSET (WASOPSET) TO SEE
# THAT THE MODE-SWITCH WAS SUCCESSFUL.

ENDOPT		CAF	TWO
		AD	POSMAX
		TS	OVCTR		# SKIP WITH C(A) = 1.
		
ENDIMU		CAF	ZERO
		TS	RUPTREG2	# 0 FOR IMU, 1 FOR OPTICS.
		
		XCH	Q
		TS	RUPTREG1
		
		INDEX	RUPTREG2
		CCS	MODECADR	# SEE IF IMUSTALL (OPTSTALL) PUT A JOB
		TC	+2		# TO SLEEP.
		TC	ENDMODE		# +0 IF NOT.
		CAF	ONE		# SET PROPER MODECADR TO 1 TO INDICATE
		INDEX	RUPTREG2	# A JOB WAS AWAKENED.
		XCH	MODECADR
		TC	JOBWAKE
		
ENDMODE		INDEX	RUPTREG2	# CHECK PROPER *WAS* REGISTER TO SEE IF
		CCS	WASKSET		#  SWITCH WAS SUCCESSFUL.
		TC	RUPTREG1	# YES - RETURN FOR MORE CHECKING IF OK.
		TC	+2		# FAILED - STILL WAITING TO SWITCH.
		TC	RUPTREG1	# MANUAL INHIBIT.
		
FAILEND		INDEX	RUPTREG2	# COMES HERE TO SIGNAL ERROR RETURN FROM
		CCS	MODECADR	#  MODE STALL ROUTINES.
		TC	+2		# JOB WAS WAKENED EARLIER - EXIT W/O INCR.
		COM			# NO WAKE-UP - SET MODECADR TO -0 FOR FAIL
LVENDMOD	INDEX	RUPTREG2
		TS	MODECADR
		TC	TASKOVER
		
GOODEND		INDEX	RUPTREG2	# COMES HERE TO SIGNAL A SUCCESSFUL SWITCH
		CCS	MODECADR
		TC	BUMPJOB		# JOB WAS AWAKENED - INCREMENT LOC.
		CS	ONE		# NO WAKE - SET MODECADR TO -1 TO INDICATE
		TC	LVENDMOD	#  SUCCESS.
		
BUMPJOB		INDEX	LOCCTR		# ARRIVES WITH C(A) = 0. LOCCTR SET TO
		XCH	LOC		#  AWAKENED JOB REGISTERS.
		AD	MINUS1		# LOC IS NEGATIVE FOR BASIC JOBS.
		INDEX	LOCCTR
		XCH	LOC
		TC	LVENDMOD	# SET MODECADR TO +0 AND EXIT.
		
## Page 202

#	WHEN A JOB WHICH REQUESTED A MODE SWITCH MUST IDLE UNTIL THE SWITCH IS COMPLETE, IT COMES TO IMUSTALL
# (OR OPTSTALL) TO WAIT FOR THE COMPLETION AND TO DO ERROR CHECKING. RETURN IS TO THE LOCATION IMMEDIATELY FOLLOW-
# ING THE CALLING SEQUENCE IF THE SWITCH WAS UNSUCCESSFUL, AND THE NEXT LOCATION IF IT WAS SUCCESSFUL. ANY
# IDLING IS DONE BY PUTTING THE JOB TO SLEEP, SO THAT A VAC AREA (IF USED) WOULD BE PRESERVED.

OPTSTALL	CAF	ONE		# 0 FOR IMU AND 1 FOR OPTICS AS USUAL.
		TC	+2
		
IMUSTALL	CAF	ZERO
		INHINT			# ONE SUB-SYSTEM AT A TIME ONLY.
		TS	RUPTREG2
		INDEX	A
		CCS	MODECADR	# SEE IF MODE SWITCH IS COMPLETE.
		TC	MODABORT	# VERY ILL IF SOMEONE ALREADY WAITING.
		TC	MODESLP		# MODE SWITCH INCOMPLETE - PUT JOB TO REST
		TC	MODEGOOD	# -1 INDICATES A SUCCESSFULLY COMPLETED SW
		
MG2		INDEX	RUPTREG2	# -0 MEANS FINISHED BUT FAILED.
		TS	MODECADR	# RESET TO +0.
		TC	MODEEXIT	# RELINT AND RETURN VIA SWCALL.
		
MODEGOOD	CCS	A		# SEE THAT MODECADR WAS INDEED -1.
		TC	MODABORT	# VERY ILL IF SOMEONE ALREADY WAITING.
		XCH	TEMQS		# INCREMENT RETURN TO INDICATE SUCCESS.
		AD	ONE
		XCH	TEMQS		# BRING +0 BACK
		TC	MG2		# TO RESET MODECADR AND EXIT.
		
MODESLP		TC	MAKECADR	# MAKE CADR FROM SWCALL RETUN ADDRESS.
		XCH	DDRWD
		INDEX	RUPTREG2
		TS	MODECADR
		TC	JOBSLEEP
		
MODABORT	TC	ABORT		# IMU OR OPTSTALL ABORT.
		OCT	01210
		
ENDSTALL	EQUALS

## Page 203

#	THE FOLLOWING ROUTINE INCREMENTS IN 2S COMPLEMENT THE REGISTER WHOSE ADDRESS IS IN ADDRWD BY THE
# QUANTITY FOUND IN TEM2.  THIS MAY BE USED TO INCREMENT DESIRED IMU AND OPTICS CDU ANGLES OR ANY OTHER 2S
# COMPLEMENT (+0 UNEQUAL TO -0) QUANTITY.

CDUINC		TS	TEM2		# 1S COMPLEMENT INCREMENT ARRIVING IN A.
		INDEX	BUF
		CCS	0		# THE 16TH BIT OF A WILL BE USED TO
		AD	ONE		# ACHIEVED THE REQUIRED 32,768 DISTINCT
		TC	+4		# STATES.
		
		AD	ONE
		AD	ONE		# A MAY HAVE OVERFLOW PRESENT HERE.
		COM			# DESIRED -1 IF DESIRED WAS NEGATIVE.
		
 +4		AD	TEM2		# AND MAYBE OVERFLOW SOME MORE.
		CCS	A		# BACK TO 2S COMPLEMENT.
		AD	ONE		#   NOTE THAT CCS TREATS A AS A 16 BIT NO.
		TC	+2
		COM
		TS	OVCTR		# REVERTS -0 TO +0.
		TC	+4		# NO OVERFLOW - PLANT NEW DESIRED.
		
		INDEX	A		# OVERFLOW - SIMULATE UN-CORRECTED SIGN.
		CAF	LIMITS		# 37777 FOR NEGATIVE - 40000 IF PLUS.
		AD	OVCTR		# OVERFLOW-CORRECTED DIFFERENCE.
		
 +4		INDEX	BUF
		TS	0		# NEW ANGLE.
		
		TC	SWRETURN	# RETURN TO CALLER.
		
## Page 204

#	MARK REQUESTING ROUTINES.

SXTMARK		INHINT
		TS	RUPTREG1	# NUMBER OF MARKS REQUESTED.
		
		CCS	MARKSTAT	# SEE IF MARK BUTTON ALREADY SNATCHED.
		TC	+2		# YES - ALARM AND END THE STRAY JOB.
		TC	MARKOK		# +0 INDICATES AVAILABLE.
		
		TC	ALARM
		OCT	00105
		TC	ENDOFJOB
		
MARKOK		CCS	VAC1USE		# FIND A VAC AREA TO STORE THE MARKS IN.
		TC	MKVACFND
		CCS	VAC2USE
		TC	MKVACFND
		CCS	VAC3USE
		TC	MKVACFND
		CCS	VAC4USE
		TC	MKVACFND
		CCS	VAC5USE
		TC	MKVACFND
		
		TC	ABORT		# VAC AREAS ALL OCCUPIED - ABORT.
		OCT	01207
		
MKVACFND	AD	TWO		# ADDRESS OF VAC.
		TS	MARKSTAT
		INDEX	A
		TS	QPRET		# USED TO SHOW NEXT AVAILABLE MARK SLOT.
		
		CAF	ZERO		# SHOW VACAREA IS OCCUPIED.
		INDEX	MARKSTAT
		TS	0 -1
		
		CAF	BIT12		# PLACE DESIRED NUMBER OF MARKS IN 12 - 14
		EXTEND
		MP	RUPTREG1
		XCH	LP
		AD	MARKSTAT	# JUST CONTAINS LOW 9 BITS OF VAC ADDRESS.
		TS	MARKSTAT
		
MARKEXIT	CAF	PRIO32
		TC	NOVAC
		CADR	MKVB51
		TC	MODEEXIT
		
## Page 205

#	MARK SYSTEM RELEASING ROUTINE.

MKRELEAS	CAF	ZERO		# SHOW MARK SYSTEM NOW AVAILABLE.
		XCH	MARKSTAT
		CCS	A
		INDEX	A
		TS	0
		
		TC	SWRETURN
		
## Page 206

#	KEYRUPT LEAD-IN AND MARK/MARK ACCEPT PROGRAMS.

KEYRUPTA	TS	BANKRUPT
MARK		CS	OPTY		# PRECISION OPTICS DRIVE - GATHER DATA
		TS	RUPTSTOR +5	# IMMEDIATELY AND THEN TRANSFER TO VAC.
		
		CS	OPTX		# SHAFT OPTICS ENCODER.
		TS	RUPTSTOR +3
		
		CS	CDUY		# READ INNER-MIDDLE-OUTER IMUCDUS.
		TS	RUPTSTOR +2
		
		CS	CDUZ
		TS	RUPTSTOR +4
		
		CS	CDUX
		TS	RUPTSTOR +6
		
		TC	READTIME +1	# SPECIAL DP TIME COUNTER-READING ROUTINE.
		
		CAF	BIT5		# DO NOT ACCEPT KEYCODE UNLESS PREVIOUS
		MASK	STATE		# KEYCODE HAS DISAPPEARED FROM IN0 (SEE
		CCS	A		# T4RUPTA). IF SO, BIT5 OF STATE = 0.
		TC	RESUME		# IF NOT, BIT5 = 1.
		
		CS	BIT5		# ACCEPT CODE AND BLOCK KEYRUPTS UNTIL
		MASK	STATE		# PRESENT KEYCODE HAS GONE AWAY.
		AD	BIT5
		TS	STATE
		
		CCS	IN0		# SEE IF KEYCODE OR MARK.
		AD	ONE
		TC	KEYCALL
		TC	+1		# IN0 SHOULD NEVER CONTAIN -0.
		
		XCH	IN3		# NOW FIND OUT IF IT WAS A MARK-REJECT.
		XCH	IN3
		MASK	BIT12		# OPT ZERO/MARK REJECT BIT.
		CCS	A
		TC	MKREJECT
## Page 207

		CCS	MARKSTAT	# SEE IF MARKS BEING CALLED FOR.
		TC	MARK2
		
		XCH	RUPTSTOR +3	# STORE IN OBTAINED MPAC COMPLEMENTED.
		TS	RUPTREG1	# OPTICS ANGLES AND MINOR PART OF TIME.
		XCH	RUPTSTOR +5	# -OPTX, -OPTY, AND -TIME1.
		TS	RUPTREG2
		XCH	RUPTSTOR +1
		TS	RUPTREG3
		
		CAF	PRIO5		# CALL SPECIAL DISPLAY JOB
		TC	NOVAC
		CADR	MARKDISP
		
		XCH	RUPTREG1	# PLANT INFORMATION IN MPAC OF REGISTER
		INDEX	LOCCTR		# SET.
		TS	MPAC
		XCH	RUPTREG2
		INDEX	LOCCTR
		TS	MPAC +1
		XCH	RUPTREG3
		INDEX	LOCCTR
		TS	MPAC +2
		
		TC	RESUME
		
## Page 208

MARK2		AD	74K		# SEE IF ANY MORE MARKS CALLED FOR.
		CCS	A
		TC	MARK3
BIT10BAR	OCT	-1000
BADMARK		TC	ALARM
		OCT	00106
		TC	RESUME		# NO FURTHER ACTION HERE.
		
MARK3		AD	ONE		# SET BIT 10 = 1 TO ENABLE MARK
		MASK	BIT10BAR	# REJECT.
		AD	BIT10
		TS	MARKSTAT
		
		MASK	LOW9		# SET UP REGISTER TRANSFER LOOP.
		TS	RUPTREG2	# ADVANCE POINTER IN QPRET.
		INDEX	A
		XCH	QPRET		# PICK UP MARK SLOT-POINTER.
		AD	SIX
		TS	RUPTREG1
		AD	ONE
		INDEX	RUPTREG2
		TS	QPRET
		CAF	SIX		# LOOP SEVEN TIMES.
		
READLOOP	TS	RUPTREG2
		INDEX	A
		CS	RUPTSTOR
		INDEX	RUPTREG1
		TS	0
		CCS	RUPTREG1	# ADDRESS NEXT LOCATION IN VAC.
		TS	RUPTREG1
		CCS	RUPTREG2
		TC	READLOOP
		
		CAF	HI5		# IF ALL REQUESTED MARKS MADE, CHANGE
		MASK	MARKSTAT	# DSKY TO VERB 50 FLASHING.
		CCS	A
		TC	RESUME
		CAF	PRIO32
		TC	NOVAC
		CADR	MKVB50
		TC	RESUME		# DONE.
		
## PagE 209

MKREJECT	CCS	MARKSTAT	# SEE IF MARKS BEING ACCEPTED.
		TC	REJECT2
		TC	ALARM		# CURSE IF NOT.
		OCT	00107
		TC	TASKOVER	# (UNTIL MK ACCEPT BUTTON AVAILABLE).
		
REJECT2		CS	BIT10		# SEE IF MARK HAD BEEN MADE SINCE LAST
		MASK	MARKSTAT	# MARK REJECT, AND SET BIT 10 TO ZERO TO
		XCH	MARKSTAT	# SHOW MARK REJECT.
		MASK	BIT10
		CCS	A
		TC	REJECT3
		
		TC	ALARM		# DONT ACCEPT TWO MARK REJECTS TOGETHER.
		OCT	00110
		TC	TASKOVER	# (UNTIL MK ACCEPT BUTTON AVAILABLE).
		
REJECT3		CAF	LOW9		# DECREMENT POINTER TO REJECT MARK.
		MASK	MARKSTAT
		TS	ITEMP1
		CS	SEVEN
		INDEX	ITEMP1
		AD	QPRET
		INDEX	ITEMP1
		TS	QPRET		# NEW POINTER.
		
		CAF	BIT12		# INCREMENT MARKS-TO-BE-ACCEPTED FIELD
		AD	MARKSTAT	# AND IF FIELD IS NOW NON-ZERO, CHANGE
		XCH	MARKSTAT	# DSKY TO VERB 51 FLASHING TO INDICATE
		MASK	HI5		# MORE MARKS REQUIRED.
		CCS	a
		TC	TASKOVER	# (UNTIL BUTTON AVAIL.)
		CAF	PRIO32
		TC	NOVAC
		CADR	REMKVB51
		TC	TASKOVER
		
## Page 210

MARKDISP	TC	GRABDSP		# SPECIAL JOB TO DISPLAY UNCALLED-FOR MARK
		TC	PREGBSY
		
REMKDSP		CS	MPAC		# THE MPAC REGISTERS CONTIN -OPTX, -OPTY,
		TS	DSPTEM1
		CS	MPAC +1
		TS	DSPTEM1 +1
		CS	MPAC +2
		TS	DSPTEM2
		CAF	ZERO
		TS	DSPTEM1 +2
		
		CAF	MKDSPCOD	# NOUN-VERB FOR MARK DISPLAY.
		TC	NVSUB
		TC	MKDSPBSY	# IF BUSY.
		
ENDMKDSP	TC	FREEDSP

		TC	 ENDOFJOB
		
MKDSPBSY	CAF	LREMKDSP	# TAKE DATA OUT OF MPAC WHEN RE-AWAKENED.
		TC	NVSUBUSY
		
MKDSPCOD	OCT	00656
LREMKDSP	CADR	REMKDSP

KEYCALL		TC	POSTJUMP	# GO TO KEYBOARD/DISPLAY BANK WITH A
		CADR	KEYRUPTC	# GO THRU STANDARD LOC.
		
74K		OCT	74000

SCTMARK		EQUALS	SXTMARK

## Page 211

#	KEYBOARD AND DISPLAY ROUTINES FOR MARK PROCEDURES.
#
#	FLASH VERB 51 WHEN WAITING FOR MARKS, CHANGING TO VERB 50 FLASHING WHEN ALL ARE IN.

MKVB51		CAF	VB51		# ASSUME USING PROGRAM HAS GRABBED DSP.
		TC	NVSUB
		TC	PRENVBSY
		TC	BANKCALL
		CADR	FLASHON
		TC	ENDIDLE
		TC	MKVB5X		# DONT RESPOND TO PROCEED OR TERMINATE.
		TC	MKVB5X
		
		CAF	OCT76		# ON ENTER, SEE IF DATA LOADED INSTEAD.
		MASK	VERBREG
		AD	-OCT50		# VERBS 50 AND 51 CAUSE END MARK ROUTINES.
		CCS	A
		TC	MKVB5X		# ON DATA LOAD, RE-DISPLAY ORIGINAL VERB.
-OCT50		OCT	-50
		TC	MKVB5X
		
		CAF	LOW9
		MASK	MARKSTAT
		TS	MARKSTAT	# VAC ADR IN MARKSTAT AND NO. MARKS MADE
		COM
		INDEX	MARKSTAT	# WILL BE LEFT IN QPRET.
		AD	QPRET
		EXTEND
		MP	BIT12
		AD	ONE
		INDEX	MARKSTAT
		TS	QPRET
		
		INHINT			# GO SERVICE OPTSTALL INTERFACE WITH 
		CAF	ONE		# USING PORGRAM.
		TC	WAITLIST
		CADR	ENDMARKS
		TC	ENDOFJOB
		
## Page 212

MKVB5X		CAF	HI5		# RE-DISPLAY VERB 51 IF MORE MARKS
		MASK	MARKSTAT	# WANTED AND VERB 50 IF ALL IN.
		CCS	A
		CAF	BIT7		# (MAKES VERB 51).
		AD	VB50
		TC	MKVB51 +1
		
ENDMARKS	TC	ENDOPT
		TC	GOODEND
		
VB50		OCT	5000
OCT76		OCT	76

#	ON RECEIPT OF LAST REQUESTED MARK, DISPLAY VERB 50 (STILL FLASHING).

MKVB50		CAF	VB50
		TS	NVTEMP		# SPECIAL ENTRY TO NVSUB WHICH AVOID BUSY
		TC	NVSUB +5	# TEST. 
VB51		OCT	5100
		TC	ENDOFJOB
		
#	IF THE ABOVE IS REJECTED, REVERT TO VERB 51.

REMKVB51	CAF	VB51
		TC	MKVB50 +1
		
## Page 213

#	THE FOLLOWING ROUTINE ALLOWS INTERLOCKING OF IMU COMPENSATION AND CERTAIN IMU MODE CHANGES WHICH ARE
# MEANINGFUL AFTER THE IMU HAS BEEN ALIGNED. THESE INCLUDE IMUFINNW, IMUATTC, AND IMUREENT.

TSTIMACT	INHINT			# TEST IMU ACTIVITY BIT TO SEE IF GYROS
		XCH	Q
		TS	MPAC +2
		CS	BIT11		# BEING TORQUED NOW.
		MASK	FLAGWRD1
		AD	BIT11
		XCH	FLAGWRD1
		MASK	BIT11
		CCS	A
		TC	+2		# IMU BUSY.
		TS	MPAC +2		# CHANNEL AVAILABLE - PROCEED AS USUAL.]
		
		XCH	BANKTEM		# PUT THE JOB TO SLEEP FOR MAX TIME IT
		TS	MPAC		# SHOULD TAKE TO COMPENSATE.
		XCH	TEMQS
		TS	MPAC +1
		CAF	320MS
		TC	WAITLIST
		CADR	IMACT2
		CAF	LIMUACTW
		TC	JOBSLEEP
		
#	WAKE UP THE JOB AND SETTHE ACTIVITY FLAG.

IMACT2		CAF	LIMUACTW
		TC	JOBWAKE
		TC	FLAG1UP
		OCT	02000
		TC	TASKOVER
		
LIMUACTW	CADR	IMUACTW

IMUACTW		INHINT
		XCH	MPAC		# PROCEED WITH MODE SWITCH.
		TS	BANKTEM
		XCH	MPAC +1
		TS	TEMQS
		TC	MPAC +2
